package net.wantongtech.xa.uploadbasicinfo.service;

import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.zwxt.stat.AccountStaticValue;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CommonBaseDao;
import net.wantongtech.xa.uploadbasicinfo.vo.PostPoneCard;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析包年顺延卡信息
 */
@Slf4j
@Service
public class ParsePostPoneCardInfoService {

    @Resource
    private CommonBaseDao commonBaseDao;

    @SuppressWarnings("UnstableApiUsage")
    public void parse(String src) {
        List<PostPoneCard> postPoneCardList = new ArrayList<>();
        try {
            postPoneCardList = Files.readLines(new File(src), Charset.forName("GB2312"), new LineProcessor<List<PostPoneCard>>() {

                private List<PostPoneCard> postPoneCardList = Lists.newArrayList();

                private int rowNumber = 1;

                @Override
                public boolean processLine(String line) {
                    if (rowNumber == 1) {
                        rowNumber++;
                        return true;
                    }
                    String[] splits = StringUtils.split(line, ",");
                    if (ArrayUtils.isNotEmpty(splits) && splits.length >= 7) {
                        PostPoneCard postPoneCard = new PostPoneCard();
                        postPoneCard.setRowNumber(Integer.valueOf(splits[0]));
                        postPoneCard.setVehPlate(splits[1]);
                        postPoneCard.setCardNo(splits[2]);
                        postPoneCard.setCancelOn(splits[3]);
                        postPoneCard.setPacketStartOn(splits[4]);
                        postPoneCard.setPacketEndOn(splits[5]);
                        postPoneCard.setPacketRoad(Integer.valueOf(splits[6]));
                        postPoneCardList.add(postPoneCard);
                    }
                    rowNumber++;
                    return true;
                }

                @Override
                public List<PostPoneCard> getResult() {
                    return postPoneCardList;
                }
            });

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        if (postPoneCardList.isEmpty()) {
            return;
        }

        for (PostPoneCard postPoneCard : postPoneCardList) {
            String vehPlate = postPoneCard.getVehPlate();
            log.info("开始处理车牌号：{}的数据", vehPlate);
            CardInfo cardInfo = commonBaseDao.queryCardInfoByVehPlate(vehPlate);
            //包年卡
            if (cardInfo != null && cardInfo.getAnnualFeeCard() != null &&
                    cardInfo.getAnnualFeeCard() == AccountStaticValue.AnnualFeeCardState.ANNUAL_CARD) {
                postPoneCard.setNewCardNo(cardInfo.getCardNetID() + cardInfo.getECardNo());
                postPoneCard.setNewPacketStartOn(DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(cardInfo.getPacketCaptureStartOn()));
                postPoneCard.setNewPacketEndOn(DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(cardInfo.getPacketCaptureEndOn()));
                postPoneCard.setNewPacketRoad(Integer.valueOf(cardInfo.getPacketCaptureRoad()));
            }
        }

        StringBuilder text = new StringBuilder();
        for (PostPoneCard postPoneCard : postPoneCardList) {
            text.append(String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s%n", postPoneCard.getRowNumber(),
                    postPoneCard.getVehPlate(), postPoneCard.getCardNo(), postPoneCard.getCancelOn(),
                    postPoneCard.getPacketStartOn(), postPoneCard.getPacketEndOn(), postPoneCard.getPacketRoad(),
                    StringUtils.trimToEmpty(postPoneCard.getNewCardNo()),
                    StringUtils.trimToEmpty(postPoneCard.getNewPacketStartOn()), StringUtils.trimToEmpty(postPoneCard.getNewPacketEndOn()),
                    postPoneCard.getNewPacketRoad() == null ? "" : postPoneCard.getNewPacketRoad()));
        }
        try {
            Files.write(text.toString(), new File("C:\\temp\\upload", "postPone.txt"), StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

}
